/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author banhbaochay
 */
public class Album {

    /**
     * json's element: _attribs
     */
    public static final String ATTRIBS = "_attribs";
    /**
     * json's element: title
     */
    public static final String TITLE = "title";
    /**
     * json's element: username
     */
    public static final String USERNAME = "username";
    /**
     * json's element: name
     */
    public static final String NAME = "name";
    /**
     * json's element: subalbum_count
     */
    public static final String SUBALBUM_COUNT = "subalbum_count";
    /**
     * json's element: photo_count
     */
    public static final String PHOTO_COUNT = "photo_count";
    /**
     * json's element: video_count
     */
    public static final String VIDEO_COUNT = "video_count";
    
    
    /**
     * Constructor create album object
     * @param album json has format:<br/>
     * <ul>
     *    <li>status</li>
     *    <li>format</li>
     *    <li>method</li>
     *    <li>timestamp</li>
     *    <li>content: 
     *        <ul>
     *              <li>album: 
     *                  <ul>
     *                       <li>_attribs:
     *                            <ul>
     *                                 <li>title</li>
     *                                 <li>username</li>
     *                                 <li>subalbum_count</li>
     *                                 <li>name</li>
     *                                 <li>photo_count</li>
     *                                 <li>privacy</li>
     *                                 <li>video_count</li>
     *                                 <li>thumb</li>
     *                            </ul>
     *                       </li>
     *                       <li>media: is array, an element contains:
     *                            <ul>
     *                                 <li>_attribs:
     *                                      <ul>
     *                                           <li>name</li>
     *                                           <li>username</li>
     *                                           <li>type</li>
     *                                           <li>description_id</li>
     *                                           <li>uploaddate</li>
     *                                           <li>public</li>
     *                                      </ul>
     *                                 </li>
     *                                 <li>isSponsored</li>
     *                                 <li>title</li>
     *                                 <li>browseurl</li>
     *                                 <li>description</li>
     *                            </ul>
     *                       </li>
     *                       <li>album: is array, an element is an album</li>
     *                  </ul>
     *              </li>
     *        </ul>
     *    </li>
     * </ul>
     */
    public Album(JSONObject album) {
        this.album = album;
        _attribs = album.optJSONObject(ATTRIBS);
    }
    
    /**
     * Get title of an album
     * @return null if error
     */
    public String getTitle() {
        if (_attribs != null) {
            return _attribs.optString(TITLE, null);
        } else {
            return null;
        }// end if _attribs != null
    }// end getTitle method
    
    /**
     * Get username who create album
     * @return null if error
     */
    public String getUsername() {
        if (_attribs != null) {
            return _attribs.optString(USERNAME, null);
        } else {
            return null;
        }// end if _attribs != null
    }// end getUsername method
    
    /**
     * Get album's name
     * @return null if error
     */
    public String getName() {
        if (_attribs != null) {
            return _attribs.optString(NAME, null);
        } else {
            return null;
        }// end if _attribs != null
    }// end subAlbumName method
    
    /**
     * Get amount sub albums in.
     * @return -1 if error
     */
    public int getSubAlbumCount() {
        if (_attribs != null) {
            return _attribs.optInt(SUBALBUM_COUNT, -1);
        } else {
            return -1;
        }// end if _attribs != null
    }// end getSubAlbumCount method
    
    /**
     * Get amount of photos in album
     * @return -1 if error
     */
    public int getPhotoCount() {
        if (_attribs != null) {
            return _attribs.optInt(PHOTO_COUNT, -1);
        } else {
            return -1;
        }// end if _attribs != null
    }// end getPhotoCount method
    
    /**
     * Set photo count to album
     * @param count amount of photos in album
     * @throws JSONException 
     */
    public void setPhotoCount(int count) throws JSONException {
        _attribs.put(PHOTO_COUNT, count);
    }// end setPhotoCount method
    
    /**
     * Get amount of videos in album
     * @return -1 if error
     */
    public int getVideoCount() {
        if (_attribs != null) {
            return _attribs.optInt(VIDEO_COUNT, -1);
        } else {
            return -1;
        }// end if _attribs != null
    }// end getPhotoCount method
    
    public boolean hasSubAlbum() {
        return (getSubAlbumCount() > 0);
    }// end hasSubAlbum method
    
    /**
     * Get list sub album
     * @return null if no sub album
     */
    public List<Album> getSubAlbumList() {
        if (getSubAlbumCount() > 0) {
            List<Album> subAlbumList = new ArrayList<Album>();
            JSONArray arr = album.optJSONArray("album");
            for (int i = 0; i < arr.length(); i++) {
                JSONObject subAlbumJSON = arr.optJSONObject(i);
                Album subAlbum = new Album(subAlbumJSON);
                subAlbumList.add(subAlbum);
            }// end for i
            return subAlbumList;
        } else {
            return null;
        }// end if album.has("album")
        
    }// end getSubAlbumList method
    
    /**
     * Get list of first <em>amout</em> media in album 
     * @param amount amount media want to get
     * @return emty list if can't get
     */
    public List<Media> getMediaList(int amount) {
        return getMediaList(0, amount);
    }// end getMediaList method
    
    /**
     * Get list of <em> amount</em> media from <em>from-th</em> media
     * @param from based on zero
     * @param amount
     * @return emty list if can't get
     */
    public List<Media> getMediaList(int from, int amount) {
        List<Media> medialList = new ArrayList<Media>(amount);
        int photoCount = getPhotoCount();
        if (photoCount > 0 && amount > 0 && from < photoCount - 1) {
            JSONArray arr = album.optJSONArray("media");
            if (from < 0) {
                from = 0;
            }// end if from < 0
            if (from + amount - 1 > photoCount - 1) {
                /*
                 * amount is set to distance between from to the end of arr
                 */
                amount = photoCount - from;
            }// end if from + amount > getPhotoCount() - 1
            for (int i = from; i < from + amount - 1; i++) {
                JSONObject mediaJSON = arr.optJSONObject(i);
                Media media = new Media(mediaJSON);
                medialList.add(media);
            }// end for i
        }
        return medialList;
    }// end getMediaList method

    @Override
    public String toString() {
        return getName() + " - Photos: " + getPhotoCount();
    }
    
    /*
     * Private member
     */
    JSONObject album;
    JSONObject _attribs;
}// end Album class

